/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comlink;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;



public abstract class MySocket{
    
    BufferedReader inFromClient;
    DataOutputStream outToClient;
    ServerSocket server;
    Thread listenThread;
    boolean listening = true;
    static MySocket test = null;
    
    
     public static void main(String[] args){
        test = new MySocket(6789){

            @Override
            public void receivedData(String line) {
                System.out.println("got line:"+line);
                if (line.equals("quit") && test != null) test.close();
                
            }
            
        };
        
        test.sendData("hello Csharp\n");
        
        test.startListening();
       
        test.sendData("goodBye csharp\n");
        
    }
    public MySocket(int port){
        try {
            
            server = new ServerSocket(port);
            System.out.println("waiting for client");
            Socket connectionSocket = server.accept();
            System.out.println("connected");
               inFromClient =
                  new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
               outToClient = new DataOutputStream(connectionSocket.getOutputStream());
              
               //System.out.println("Received: " +  inFromClient.readLine());
               outToClient.writeBytes("hello client\n");
               
        } catch (IOException ex) {
            Logger.getLogger(MySocket.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        
    }
    public void startListening(){
        listening = true;
        listenThread = new Thread(){

            @Override
            public void run() {
                while(listening){
                    try {
                        receivedData(inFromClient.readLine());
                        
                    } catch (IOException ex) {
                        System.out.println("connection stopped...");
                        return;
                    }
                }
            }
          
            
        };
        listenThread.start();
    }
    
    
    public void close(){
        listening = false;
        try {
            if (server!=null)server.close();
        } catch (IOException ex) {
            Logger.getLogger(MySocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public boolean sendData(String line){
        try {
            outToClient.writeBytes(line);
            outToClient.flush();
        } catch (IOException ex) {
            Logger.getLogger(MySocket.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
    public abstract void receivedData(String line);

    public boolean isConnected() {
        if (server == null || server.isClosed()){
            return false;
        }else{
            return true;
        }
    }
}
